package org.devf.fork.imsimulator.socket.handler;

import cn.hutool.json.JSONUtil;
//import cn.hutool.log.Log;
import org.devf.fork.imsimulator.model.UserStatus;
import org.devf.fork.imsimulator.play.MusicPlayer;
import org.devf.fork.imsimulator.play.TipAudioEnum;
import org.devf.fork.imsimulator.service.vo.UserExtendModel;
import org.devf.fork.imsimulator.socket.Channel;
import org.devf.fork.imsimulator.socket.Ihandler;
import org.devf.fork.imsimulator.socket.dto.BasePackets;
import org.devf.fork.imsimulator.socket.dto.BizType;
import org.devf.fork.imsimulator.socket.handler.listener.FriendStateListener;
import org.devf.fork.imsimulator.socket.handler.listener.HandlerListener;

import java.util.Objects;

/**
 * FriendOnlineHandler related
 *
 * @author wanghuafu
 * @since 1.0  2023/9/12 14:37
 */
public class FriendOnlineHandler implements Ihandler {

    private FriendStateListener friendStateListener;

    @Override
    public boolean isSupport(BasePackets packets) {
        return packets.getBizType().equals(BizType.FRIEND_ON_LINE.name());
    }

    @Override
    public void register(HandlerListener chatListener) {
        friendStateListener = (FriendStateListener) chatListener;
    }

    @Override
    public boolean execute(Channel channel, BasePackets packets) {
//        Log.get().info("有好友状态变更");
        UserExtendModel friend = JSONUtil.toBean(packets.getContents().toString(),UserExtendModel.class);

        //播放声音
        if (Objects.equals(friend.getOnlineStatus(), UserStatus.ONLINE.getCode())){
            MusicPlayer.play(TipAudioEnum.FRIEND_ON_LINE);
//            Log.get().info(friend.getNikname()+"上线了");
        }

        if (friendStateListener!=null){
            friendStateListener.friendOnline(friend);
        }
        return true;
    }
}
